import { createRouter, createWebHistory } from 'vue-router'





const router = createRouter({
    //路由模式设置
    history: createWebHistory(),
    //管理路由
    routes: [
        {
            path: '/navbar',
            component: () => import('@/pages/navbar/index.vue'),
            children: [
                {
                    path: '/navbar',
                    component: () => import('@/pages/navbar/article/articleList.vue'),
                },
                {
                    path: '/favorite',
                    component: () => import('@/pages/navbar/favorite/favorite.vue'),
                },
                {
                    path: '/article',
                    component: () => import('@/pages/navbar/article/article.vue'),
                },
                {
                    path: '/travel',
                    component: () => import('@/pages/navbar/travel/travel.vue'),
                },
                {
                    path: '/friend',
                    component: () => import('@/pages/navbar/friend/friend.vue'),
                },
                {
                    path: '/funny',
                    component: () => import('@/pages/navbar/music/music.vue'),
                },
                {
                    path: '/sort',
                    component: () => import('@/pages/navbar/sort/sort.vue'),
                },
                {
                    path: '/user',
                    component: () => import('@/pages/navbar/user/user.vue'),
                },
                {
                    path: '/articlePublish',
                    component: () => import('@/pages/navbar/articlePublish/articlePublish.vue'),
                    meta: { requiresAuth: true },
                },
                {
                    path: '/audiopublish',
                    component: () => import('@/pages/navbar/audioPublish/audioPublish.vue'),
                },
                {
                    path: '/voice',
                    component: () => import('@/pages/navbar/voice/voice.vue'),
                },
                {
                    path: '/myArticle',
                    component: () => import('@/pages/navbar/myArticle/myArticle.vue'),
                },
            ]


        },
        {
            path: '/ranking',
            component: () => import('@/pages/ranking/index.vue'),
        },
        {
            path: '/',
            redirect: '/navbar'
        },
        {
            path: '/home',
            component: () => import('@/pages/home/indexa.vue'),
       
        },


        {
            path: '/login',
            component: () => import('@/pages/login/index.vue'),
        },
        {
            path: '/wxlogin',
            component: () => import('@/pages/login/wxlogin.vue'),
        },
        {
            path: '/star',
            component: () => import('@/pages/three/composer/star.vue'),
        },
        {
            path: '/callback',
            component: () => import('@/pages/login/components/wxcallback.vue'),
        },
        
       
    ],
    //控制滚动行为
    scrollBehavior() {
        return {
            left: 0,
            top: 0
        }
    }
})


router.beforeEach((to, from, next) => {
    const loggedIn = checkIfUserIsLoggedIn(); // 这里需要实现一个检测用户是否登录的方法
  
    if (to.matched.some(record => record.meta.requiresAuth) && !loggedIn) {
      next('/login'); // 如果需要登录验证且用户未登录，则重定向到登录页面
    } else {
      next(); // 其他情况放行
    }
  });

  const checkIfUserIsLoggedIn =()=>{
    const currentUser = localStorage.getItem('currentUser')
    return currentUser
  }


  export default router;